<?php

namespace Admin\Model\DBO;

use Zend\Db\TableGateway\AbstractTableGateway;
use Admin\Model\DAO\BookStoreTable;

class SearchBookDBO extends AbstractTableGateway {

    private $sm;
    private $authorTable;
    private $bookStoreTable;
    private $serialBookTable;

    public function __construct($serviceManager) {
        $this->sm = $serviceManager;
    }

    public function getBookStoreTable() {
        if (!$this->bookStoreTable) {
            $this->bookStoreTable = $this->sm->get('Admin\Model\DAO\BookStoreTable');
        }
        return $this->bookStoreTable;
    }

    public function getAuthorTable() {
        if (!$this->authorTable) {
            $this->authorTable = $this->sm->get('Admin\Model\DAO\AuthorTable');
        }
        return $this->authorTable;
    }
    public function getSerialBookTable() {
        if (!$this->serialBookTable) {
            $this->serialBookTable = $this->sm->get('Admin\Model\DAO\SerialBookTable');
        }
        return $this->serialBookTable;
    }

    public function searchBookBarcode($bookBarcode) {
        return $this->getBookStoreTable()->searchBookBarcode($bookBarcode);
    }

    public function searchBookAuthor($valueSearch) {
        $bookBarcode = (int) $valueSearch;
        if ($bookBarcode != 0) {
            return $this->getAuthorTable()->getAuhorFromBarcode($bookBarcode);
        } else {
            return $this->getAuthorTable()->getAuthorFormName($valueSearch);
        }
    }
    // tra ve mot resultSet dang paginator
    public function searchBookName($valueSearch) {
        return $this->getBookStoreTable()->searchBookName($valueSearch);
    }
    public function searchBookPublisher($valueSearch) {
        return $this->getBookStoreTable()->searchBookPublisher($valueSearch);
    }
    public function searchBookCategory($valueSearch) {
         return $this->getBookStoreTable()->searchBookCategory($valueSearch);
    }
    public function searchBookDetail($bookBarcode) {
        $resultSet = $this->getBookStoreTable()->getBookDetail($bookBarcode);
         $objectSearchBook = new SearchBookDBO($this->sm);
         $allAuthor = array();
        // Lấy toàn bộ tác giả của cuốn sách
        foreach ($resultSet as $key) {
            $stringAuthor = "";
            $authors = $objectSearchBook->searchBookAuthor($key['bookBarcode']);
            foreach ($authors as $author) {
                $stringAuthor .= $author['author'] . '-';
            }
            $allAuthor[$key['bookBarcode']] = $stringAuthor;
        }
        $iteratorAdapter = new \Zend\Paginator\Adapter\Iterator($this->getBookStoreTable()->getBookDetail($bookBarcode));
        $paginator = new \Zend\Paginator\Paginator($iteratorAdapter);
        // Lay tat ca sach hien co trong thu vien
        $allBook = $this->getSerialBookTable()->getBookExist($bookBarcode);
        return array(
            'paginator' => $paginator,
            'allAuthor' => $allAuthor,
            'allBook' => $allBook,
        );
    }
}

?>